import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yp/config/index.dart';
import 'package:yp/config/style.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yp/services/api.dart';
import 'package:yp/utils/util.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:yp/widgets/index.dart';

import '../../router.dart';

/// 拼团热销榜
class SalesRankings extends StatefulWidget {
  const SalesRankings({Key key}) : super(key: key);

  @override
  _SalesRankingsState createState() => _SalesRankingsState();
}

class _SalesRankingsState extends State<SalesRankings>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  List hotData=[];
  ScrollController _scrollController = ScrollController();
  double _headerOpacity = 0;
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
    _scrollController.addListener(() {
      double offset = _scrollController.offset;
      if (offset < 300.w) {
        _headerOpacity = offset / 300.w;
        setState(() {});
      }
    });
    var shop_id = Get.arguments!=null?Get.arguments['shop_id']:102002001;
    var params = {"data_sign":0,"channel_num_id":98,"use_platform":3,"shop_id":shop_id,"buy_type":2,"cat1_id":null};
    Api.queryhotproduct(params).listen((event) {
      if (event['code'] == 0) {

        if (event['buy_id_list'] != null && event['buy_id_list'].length > 0) {
          setState(() {
               hotData = event['buy_id_list'];
          });

        }
      }


    });
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
        BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width,
            maxHeight: MediaQuery.of(context).size.height),
        designSize: Size(750, 1334),
        orientation: Orientation.portrait);
    return Scaffold(
      body: NestedScrollView(
        controller: _scrollController,
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return [
            SliverAppBar(
              floating: true,
              snap: true,
              brightness: Brightness.dark,
              expandedHeight: 300.w,
              backgroundColor: EsColors.Title,
              title: Opacity(
                opacity: _headerOpacity,
                child: Text(
                  '拼团热销榜',
                  style: TextStyle(
                      fontSize: EsSizes.MD * 2.sp,
                      color: EsColors.White,
                      fontWeight: EsFontWeights.Medium),
                ),
              ),
              flexibleSpace: FlexibleSpaceBar(
                background: Image(
                  image: AssetImage('assets/images/sales_rankings_header.png'),
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ];
        },
        body: ListView.builder(
          padding: EdgeInsets.only(bottom: 30.w),
          itemBuilder: (BuildContext context, int index) {
            return _item(index);
          },
          itemCount: hotData.length>10?10: hotData.length,
        ),
      ),
    );
  }

  Widget _item(int index) {
    return Container(
      margin: EdgeInsets.fromLTRB(20.w, 26.w, 20.w, 0),
      padding: EdgeInsets.all(20.w),
      decoration: EsDecoration.BoxShadowWithRadius,
      alignment: Alignment.center,
      child: Row(
        children: [
          Stack(
            children: [
              CachedImage(
                  width: 260.w,
                  height: 260.w,
                  path:
               hotData[index]['image_default_id'].toString()),
              Positioned(
                  left: 20.w,
                  top: 20.w,
                  child: Container(
                    height: 48.w,
                    width: 36.w,
                    alignment: Alignment.center,
                    child: Text(
                      index > 2 ? (index + 1).toString() : '',
                      style: TextStyle(
                          color: EsColors.ContentSecondary,
                          fontSize: EsSizes.XS * 2.sp,
                          fontWeight: EsFontWeights.Medium),
                    ),
                    decoration: BoxDecoration(
                        image: DecorationImage(
                      image: AssetImage('assets/images/' +
                          (index > 2
                              ? 'hot_top_normal'
                              : ('hot_top' + (index + 1).toString())) +
                          '.png'),
                    )),
                    // child: Image(
                    //   image: AssetImage('assets/images/' +
                    //       (index > 2
                    //           ? 'hot_top_normal'
                    //           : ('hot_top' + (index + 1).toString())) +
                    //       '.png'),
                    // ),
                  )),
            ],
          ),
          SizedBox(width: 24.w),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Container(
                    constraints: BoxConstraints(minHeight: 140.w),
                    child: Text(hotData[index]['item_title'].toString(),
                        style: TextStyle(
                            color: EsColors.Title,
                            fontSize: EsSizes.SM * 2.sp,
                            fontWeight: EsFontWeights.Normal))),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Padding(
                              padding: EdgeInsets.only(top: 4.w, right: 10.w),
                              child: EsOutLineButton(
                                '${hotData[index]['g_buy_size']}人团',
                                textColor: EsColors.Money,
                                borderColor: EsColors.Money,
                                fontSize: EsSizes.XXXS * 2.sp,
                                padding: EdgeInsets.symmetric(
                                    vertical: 4.w, horizontal: 8.w),
                              ),
                            ),
                            Text('已团${hotData[index]['buy_qty']}件',
                                style: TextStyle(
                                    color: EsColors.ContentSecondary,
                                    fontSize: EsSizes.XS * 2.sp,
                                    fontWeight: EsFontWeights.Normal)),
                          ],
                        ),
                        Row(
                          children: [
                            Padding(
                              padding: EdgeInsets.only(top: 4.w, right: 10.w),
                              child: Text('￥${hotData[index]['g_buy_price']}',
                                  style: TextStyle(
                                      color: EsColors.Primary,
                                      fontSize: EsSizes.XL * 2.sp,
                                      fontWeight: EsFontWeights.Normal)),
                            ),
                            Text('￥${hotData[index]['price']}',
                                style: TextStyle(
                                    decoration: TextDecoration.lineThrough,
                                    color: EsColors.ContentSecondary,
                                    fontSize: EsSizes.XS * 2.sp,
                                    fontWeight: EsFontWeights.Normal)),
                          ],
                        ),
                      ],
                    ),
                    Expanded(child: SizedBox()),
                    EsOutLineButton(
                      '去拼团',
                      bgColor: EsColors.Primary,
                      textColor: EsColors.White,
                      fontSize: EsSizes.MD * 2.sp,
                      fontWeight: EsFontWeights.Medium,
                      borderRadius: 32.w,
                      padding: EdgeInsets.symmetric(
                          vertical: 10.w, horizontal: 24.w),
                      onTap: () {
                        var shop_id = Get.arguments!=null?Get.arguments['shop_id']:null;
                        gopintuan(hotData[index]['g_buy_id'],shop_id);
                      },
                    )
                  ],
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

}
